package org.darkforest.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.WxJSSdkService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.SerialNumGenerator;
import org.darkforest.dao.mapper.PdLogMapper;
import org.darkforest.dao.mapper.PdRechargeMapper;
import org.darkforest.model.Member;
import org.darkforest.model.PdLogSearchBean;
import org.darkforest.model.PdLogVo;
import org.darkforest.model.PdRecharge;
import org.darkforest.model.RestEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "wallet")
@Api(value = "wallet", description = "我的钱包",produces= MediaType.APPLICATION_JSON_VALUE)
public class WalletController {

    @Autowired
    private PdLogMapper pdLogMapper;
    @Autowired
    private PdRechargeMapper pdRechargeMapper;

    @Autowired
    private WxJSSdkService wxJSSdkService;


    @AuthPassport
    @RequestMapping(value = "pdlogList",method = RequestMethod.GET)
    @ApiOperation(value = "账号明细列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity pdlogList(@CurrentMember Member member,
                                @RequestParam(required = false, defaultValue = "0") Integer startPage,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        try{

            PdLogSearchBean pdLog = new PdLogSearchBean();
            pdLog.setMemberId(member.getMemberId());
            pdLog.setStart(startPage == null ? 1 : startPage);
            pdLog.setPageSize(pageSize);
            List<PdLogVo> pbgList= pdLogMapper.selectByCondition(pdLog);

            return RestEntity.success(pbgList);
        }catch (Exception e){
            e.printStackTrace();
            return  RestEntity.failed("获取异常");
        }
    }


    @AuthPassport
    @RequestMapping(value = "doRecharge", method = RequestMethod.POST)
    @ApiOperation(value = "钱包充值", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity doRecharge(@CurrentMember Member member, String openId,
                                 @RequestParam(required = false, defaultValue = "0.00") String pdrAmount) {
        try {
            PdRecharge pdRecharge = new PdRecharge();
            Long pdrsn = SerialNumGenerator.getOrderSn(SerialNumGenerator.RECHARGE_WEBCHART);
            pdRecharge.setPdrSn(pdrsn);
            pdRecharge.setPdrMemberId(member.getMemberId());
            pdRecharge.setPdrMemberName(member.getMemberName());
            // 充值金额
            pdRecharge.setPdrAmount(new BigDecimal(pdrAmount));
            // 支付状态 0未支付1支付
            pdRecharge.setPdrPaymentState("0");
            // 添加时间
            pdRecharge.setPdrAddTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
            // 支付时间
            pdRecharge.setPdrPaymentTime(0);
            // pdrPaymentCode、 pdrPaymentState、pdrPaymentTime 充值后再更新
            pdRechargeMapper.insert(pdRecharge);

            Map<String, String> wxPayParamMap = wxJSSdkService.getJsApiPayForRecharge(openId, pdRecharge);

            TreeMap signData = new TreeMap();
            signData.put("appId", wxPayParamMap.get("appId"));
            signData.put("timeStamp", wxPayParamMap.get("timeStamp"));
            signData.put("nonceStr", wxPayParamMap.get("nonceStr"));
            signData.put("wxPackage", wxPayParamMap.get("package"));
            signData.put("signType", wxPayParamMap.get("signType"));
            signData.put("signature", wxPayParamMap.get("signature"));
            signData.put("goodsName", wxPayParamMap.get("goodsName"));
            signData.put("paySign", wxPayParamMap.get("paySign"));
            return RestEntity.success(signData);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
